class Solution {
public:
    void dfs(vector<vector<int>>& rooms,vector<int>& used,int cur){
        if(used[cur] == 1) return;
        used[cur] = 1; 
        for(int i = 0;i < rooms[cur].size();i++){
            dfs(rooms,used,rooms[cur][i]);
        }
    }
    
    bool bfs(vector<vector<int>>& rooms){
        vector<int> used(rooms.size(),0);
        queue<int> qe;
        qe.push(0);
        while(!qe.empty()){
            int cur = qe.front();
            qe.pop();
            used[cur] = 1;
            for(int i = 0;i < rooms[cur].size();i++){
                if(used[rooms[cur][i]] == 1) continue;
                qe.push(rooms[cur][i]);
            }
        }
        for(int i = 0;i < used.size();i++){
            if(used[i] == 0) {
                return false;
            }
        }
        return true;
    }
    
    bool canVisitAllRooms(vector<vector<int>>& rooms) {
        // vector<int> used(rooms.size(),0);
        // dfs(rooms,used,0);
        // for(int i = 0;i < used.size();i++){
        //     if(used[i] == 0) {
        //         return false;
        //     }
        // }
        // return true;
        return bfs(rooms);
    }   
};
